
import chilkat
import M2Crypto
import hashlib
import base64



msg = open('cadenaOriginal.txt').read()
print '\nmsg:%s\n%s' % (type(msg), msg)

msg_md5 = hashlib.md5(msg).digest()
print "\nmd5 :%s\n%s" % (type(msg_md5), msg_md5)


print '\n----------------------'


der_key = chilkat.CkPrivateKey()
der_key.LoadPkcs8EncryptedFile('aaa010101aaa_csd_08.key', 'a0123456789')
print '\nder_key:%s\n%s' % (type(der_key), der_key)

pem_key = der_key.getRsaPem()
print '\npem_key:%s\n%s' % (type(pem_key), pem_key)


print '\n----------------------'


rsa_key = M2Crypto.RSA.load_key_string(pem_key)
print '\nrsa_key:%s\n%s' % (type(rsa_key), rsa_key)

signed = rsa_key.sign(msg_md5, 'md5')
print '\nsigned:%s\n%s' % (type(signed), signed)

signed64 = base64.b64encode(signed)
print '\nsigned64 :%s\n%s' % (type(signed64), signed64)


print '\n----------------------'


encrypted = rsa_key.private_encrypt(msg_md5, M2Crypto.RSA.pkcs1_padding)
#~ print '\nencrypted:%s\n%s' % (type(encrypted), encrypted)

encrypted64 = base64.b64encode(encrypted)
print '\nencrypted64 :%s\n%s' % (type(encrypted64), encrypted64)


print '\n----------------------'


evp_key = M2Crypto.EVP.PKey(md='md5')
evp_key.assign_rsa(rsa_key)
print '\nevp_key:%s\n%s' % (type(evp_key), evp_key)

print evp_key.get_rsa()
#~ evp_key.save_key('fur2', None)


msg_dgst = M2Crypto.EVP.MessageDigest('md5')
msg_dgst.update(msg)
dgst = msg_dgst.digest()
print '\ndgst :%s\n%s' % (type(dgst), dgst)

dgst64 = base64.b64encode(msg)
print '\ndgst64 :%s\n%s' % (type(dgst64), dgst64)




